from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# https://matplotlib.org/basemap/users/merc.html

m = Basemap(projection='merc',
            resolution='c',
            area_thresh=0.01,
            llcrnrlon=118, llcrnrlat=23,
            urcrnrlon=125, urcrnrlat=28)
# m.drawcoastlines()  # 绘制海岸线
# m.drawmapboundary()

train = pd.read_hdf('../input/train.h5')

xp = np.linspace(0, 1, 1000)
yv = []
xv = []
for q in xp:
    xv += [np.quantile((train['x']/200000), q)]
    print(q, ' : ', np.quantile((train['x']/200000), q))
for q in xp:
    yv += [np.quantile((train['y']/50000), q)]
    print(q, ' : ', np.quantile((train['y']/50000), q))
plt.plot(xp, xv, '*')
plt.show()

plt.plot(xp, yv, '*')
plt.show()


# train.drop_duplicates(['ship']).shape
# train.drop_duplicates(['ship', 'type']).shape

for type in ['围网', '刺网', '拖网']:
    ships = train.loc[train['type'] == type, 'ship'].unique()
    for i in range(50):
        ship = ships[i]
        data0 = train.loc[(train['ship'] == ship) & (train['ship'] == ship), :]
        days = sorted(data0['time'].apply(lambda x: x[:4]).unique())
        lon, lat = data0['y']/50000, data0['x']/200000
        x1, x2 = min(lon), max(lon)
        y1, y2 = min(lat), max(lat)
        plt.figure()
        plt.plot(lon, lat,  '*')
        plt.title(ship)
        plt.xlim([98, 113])
        plt.ylim([28, 36])
        # plt.xlim([x1, x2])
        # plt.ylim([y1, y2])
        # plt.show()
        plt.savefig(f'../data2/{type}/{ship}.png')

    # for day in days:
    #     data1 = data0.loc[data0['time'].apply(lambda x: x[:4]) == day, :]
    #     lat = data1['x'] / 200000.0  # 获取维度之维度值
    #     lon = data1['y'] / 50000.0  # 获取经度值
    #
    #     plt.plot(lon, lat, '*')
    #     plt.xlim([x1, x2])
    #     plt.ylim([y1, y2])
    #     plt.title(str(ship)+'_'+day)
    #     # plt.show()
    #     plt.savefig(f'../data/{type}/{ship}_{day}.png')

# x, y = m(lon.values, lat.values)
# m.scatter(x, y, edgecolors='r', facecolors='r', marker='*')
# plt.title('Sea')
# plt.xlabel('Longitude')
# plt.ylabel('Latitude')
# plt.show()
